package com.qding.park.manager.service.core.facade.impl.common;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qding.park.common.core.exceptions.ExceptionMessage;
import com.qding.park.common.core.exceptions.business.manager.service.ServiceCoreException;
import com.qding.park.common.utils.DateUtils;
import com.qding.park.common.utils.ListUtil;
import com.qding.park.manager.common.pojo.ParkDataResult;
import com.qding.park.manager.facade.core.service.common.InoutCommonFacade;
import com.qding.park.manager.pojo.TbCar;
import com.qding.park.manager.pojo.TbInoutRecord;
import com.qding.park.manager.pojo.TbParkspace;
import com.qding.park.manager.pojo.TbUserInfo;
import com.qding.park.manager.pojo.enums.UserTypeEnum;
import com.qding.park.manager.service.core.mapper.TbCarMapper;
import com.qding.park.manager.service.core.mapper.TbInoutRecordMapper;
import com.qding.park.manager.service.core.mapper.TbInvitecardMapper;
import com.qding.park.manager.service.core.mapper.TbParkspaceMapper;
import com.qding.park.manager.service.core.mapper.TbReserveMapper;
import com.qding.park.manager.service.core.mapper.TbUserInfoMapper;

@Service("inoutCommonFacade")
public class InoutCommonFacadeImpl implements InoutCommonFacade {

    private static final Logger _log = LoggerFactory.getLogger(InoutCommonFacadeImpl.class);
    /**
     * @Fields tbParkspaceMapper : TODO(用一句话描述这个变量表示什么)
     */
    @Autowired
    private TbParkspaceMapper tbParkspaceMapper;

    /**
     * @Fields tbCarMapper : TODO(用一句话描述这个变量表示什么)
     */
    @Autowired
    private TbCarMapper tbCarMapper;

    @Autowired
    private TbInoutRecordMapper tbInoutRecordMapper;

    @Autowired
    private TbInvitecardMapper tbInvitecardMapper;
    @Autowired
    private TbReserveMapper tbReserveMapper;
    @Autowired
    private TbUserInfoMapper tbUserInfoMapper;

    @Override
    public void inCheck(TbInoutRecord record) {
        // TODO Auto-generated method stub
        int supplyID = record.getSupplyID();
        String parkCode = record.getParkCode();
        String carNum = record.getCarNum();

        List<TbCar> userCars = tbCarMapper.queryCarOfParkByCarNum(carNum);
        if (ListUtil.isEmpty(userCars)) {
            // 不是必要的通行记录
            _log.info("不是必要的通行记录");
            return;
        } else {
            TbParkspace tbParkspace = tbParkspaceMapper.queryParkInfoByCodeSupplier(parkCode, supplyID);

            int parkId = tbParkspace.getId();
            String parkName = tbParkspace.getName();
            record.setParkID(parkId);
            record.setParkName(parkName);
            for (TbCar car : userCars) {
                int userId = car.getUserID();
                record.setUserID(userId);
                tbInoutRecordMapper.save(record);
            }
            return;
        }

    }

    /*
     * (非 Javadoc) 车辆出场业务
     * @param record
     * @see com.qding.park.manager.facade.core.service.InoutCheckFacade#outCheck(com.
     * qding.park.manager.pojo.TbInoutRecord)
     */
    @Override
    public void outCheck(TbInoutRecord record) throws ServiceCoreException {
        // TODO Auto-generated method stub
        TbParkspace tbParkspace = tbParkspaceMapper.queryParkInfoByCodeSupplier(record.getParkCode(),
                record.getSupplyID());

        if (tbParkspace == null) {
            // 车辆没有停在此车场
            throw new ServiceCoreException(ServiceCoreException.TypeEnum.EXCEPTION_NOFIND_PARK.getId(), null,
                    ExceptionMessage.createExceptionMessage(
                            ServiceCoreException.TypeEnum.EXCEPTION_NOFIND_PARK.getDesc(), "没有找到对应的车场信息",
                            "parkCode=" + record.getParkCode() + " ,supplyID=" + record.getSupplyID()));
        }
        int parkId = tbParkspace.getId();
        String parkName = tbParkspace.getName();

        record.setParkID(parkId);
        record.setParkName(parkName);

        // 更新对应的出场记录
        tbInoutRecordMapper.outCheck(record);

        /****** 更新邀请单 ****************/
        // 更新邀请单属性
        // tbInvitecardMapper.inviteOutcheck(record);
        /******************************/

        /******* 更新预定单 *********************/
        // 更新预定单记录
        // tbReserveMapper.reserveOutcheck(record);
        /***************************/
    }

    @Override
    public ParkDataResult recordList(String bjuserid, int jumpPage, int pageLength) {

        // TbUserInfo tbUserInfo = tbUserInfoMapper.queryUserByBjUserId(bjuserid);
        TbUserInfo tbUserInfo = tbUserInfoMapper.queryUseByBjuseridType(bjuserid, UserTypeEnum.REGISTER_USER.getType());
        int userId = tbUserInfo.getId();
        PageHelper.startPage(jumpPage, pageLength);

        List<TbInoutRecord> tbInoutRecords = tbInoutRecordMapper.listByUserId(userId,
                DateUtils.addMonths(new Date(), 1));

        PageInfo page = new PageInfo(tbInoutRecords);

        List dataList = new ArrayList();
        for (TbInoutRecord record : tbInoutRecords) {
            Map<String, Object> map = new HashMap<String, Object>();

            map.put("recordId", record.getId());
            map.put("carNum", record.getCarNum());
            map.put("parkId", record.getParkID());
            map.put("parkName", record.getParkName());
            map.put("incheckTime", record.getCheckInTime() == null ? 0 : record.getCheckInTime().getTime() / 1000);
            map.put("outcheckTime", record.getCheckOutTime() == null ? 0 : record.getCheckOutTime().getTime() / 1000);
            dataList.add(map);
        }

        Map<String, Object> data = new HashMap<String, Object>();
        data.put("list", dataList);
        data.put("currentPage", page.getPageNum());
        data.put("numPerPage", page.getPageSize());
        data.put("totalCount", page.getTotal());
        data.put("hast", page.isHasPreviousPage());
        data.put("next", page.isHasNextPage());

        ParkDataResult parkDataResult = ParkDataResult.createObjDate();
        parkDataResult.putMapOnObj(data);
        return parkDataResult;
    }

    @Override
    public ParkDataResult passDel(int recordId) {
        tbInoutRecordMapper.passDel(recordId);
        return ParkDataResult.createEmpty();
    }

}
